package com.snowcattle.game.service.net; /** * Created by jiangwenping on 17/2/7. */ import com.snowcattle.game.common.constant.GlobalConstants; import com.snowcattle.game.service.net.message.encoder.NetProtoBufMessageTCPEncoder; import com.snowcattle.game.service.net.message.decoder.NetProtoBufMessageTCPDecoder; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.LengthFieldBasedFrameDecoder; import io.netty.handler.logging.LogLevel; import io.netty.handler.logging.LoggingHandler; import io.netty.handler.timeout.IdleStateHandler; /** * Created by jwp on 2017/1/26. * * LengthFieldBasedFrameDecoder 使用 * head+messagelength+serial+body * * 参数maxFrameLength 为数据帧最大长度 * 参数lengthFieldOffset为version长度表示 从第几个字段开始读取长度,表示同意为head的长度 * 参数lengthFieldLength表示占用了多少个字节数 具体可查看LengthFieldBasedFrameDecoder的getUnadjustedFrameLength方法 * 参数lengthAdjustment表示还需要拓展长度,具体表示为serial的长度 * 参数initialBytesToStrip表示 传递给下个coder的时候跳过多少字节 如果从0开始为 head+messagelength+serial+body全部给下个coder */ public class GameNetProtoMessageTcpServerChannleInitializer extends ChannelInitializer<NioSocketChannel> { @Override protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception { ChannelPipeline channelPipLine = nioSocketChannel.pipeline(); int maxLength = Integer.MAX_VALUE; channelPipLine.addLast("frame", new LengthFieldBasedFrameDecoder(maxLength, 2, 4, 0, 0)); channelPipLine.addLast("encoder", new NetProtoBufMessageTCPEncoder()); channelPipLine.addLast("decoder", new NetProtoBufMessageTCPDecoder()); // int readerIdleTimeSeconds = GlobalConstants.Net.SESSION_HEART_READ_TIMEOUT; // int writerIdleTimeSeconds = GlobalConstants.Net.SESSION_HEART_WRITE_TIMEOUT; int readerIdleTimeSeconds = GlobalConstants.Net.SESSION_HEART_ALL_TIMEOUT; int writerIdleTimeSeconds = GlobalConstants.Net.SESSION_HEART_ALL_TIMEOUT; int allIdleTimeSeconds = GlobalConstants.Net.SESSION_HEART_ALL_TIMEOUT; channelPipLine.addLast("idleStateHandler", new IdleStateHandler(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds)); channelPipLine.addLast("logger", new LoggingHandler(LogLevel.DEBUG)); channelPipLine.addLast("handler", new GameNetMessageTcpServerHandler()); } }